package com.cos.hospital.ui.patient;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.init.BaseActivity;
import com.cos.hospital.model.UHttp;
import com.cos.hospital.model.bean.ErrorBean;
import com.cos.hospital.model.bean.ResultBean;
import com.cos.hospital.model.connector.HttpMsgListener;
import com.cos.hospital.tool.SPUtil;
import com.cos.hospital.tool.UDate;
import com.cos.hospital.tool.USnackbar;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.ui.record.RecordEditActivity;
import com.cos.hospital.widget.ProgressDialog;
import com.google.gson.JsonObject;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;

import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 补写病历（选择时间）
 */
public class ReplenishRecordsTimeActivity extends BaseActivity {
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.calendarView)
    CalendarView calendarView;
    private int fldPatientId;

    @Override
    protected int layout() {
        return R.layout.activity_replenish_records_time;
    }

    @Override
    protected void init() {
        fldPatientId = getIntent().getIntExtra(Code.SP.ID, 0);
        setTitleLeftIcon();
        setTitleCenterTxt("请选择就诊日期");
        calendarView.setOnMonthChangeListener((year, month) -> tv_time.setText(year + "-" + month));
        calendarView.setThemeColor(ContextCompat.getColor(this, R.color.colorPrimary), ContextCompat.getColor(this, R.color.T_all));
        calendarView.setOnCalendarInterceptListener(new CalendarView.OnCalendarInterceptListener() {
            @Override
            public boolean onCalendarIntercept(Calendar calendar) {
                return calendar.getTimeInMillis() / UDate.DAY >= System.currentTimeMillis() / UDate.DAY;
            }

            @Override
            public void onCalendarInterceptClick(Calendar calendar, boolean isClick) {

            }
        });
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        java.util.Calendar calendar_yesterday = UDate.dateToCalendar(new Date(calendar.getTimeInMillis() - UDate.DAY));
        int yearOfCalendar = calendar_yesterday.get(java.util.Calendar.YEAR);
        int monthOfYear = calendar_yesterday.get(java.util.Calendar.MONTH) + 1;
        int dayOfMonth = calendar_yesterday.get(java.util.Calendar.DAY_OF_MONTH);
        calendarView.scrollToCalendar(yearOfCalendar, monthOfYear, dayOfMonth, true);
    }

    @Override
    protected void logic() {

    }

    @OnClick({R.id.title_back, R.id.tv_time, R.id.tv_next})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.tv_time:
//                calendarView.showYearSelectLayout(yearOfCalendar);
                break;
            case R.id.tv_next:
                Calendar selectedCalendar = calendarView.getSelectedCalendar();
                if (selectedCalendar == null) {
                    USnackbar.showError(this, "请选择就诊日期");
                    return;
                }
                String time = selectedCalendar.getYear() + "-" + selectedCalendar.getMonth() + "-" + selectedCalendar.getDay();
                if ("0-0-0".equals(time)) {
                    USnackbar.showError(this, "请选择就诊日期");
                    return;
                }
                reception(time);
                break;
        }
    }

    private void reception(String time) {
        ProgressDialog.getInstance().startProgressDialog(this);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("fldPatientId", fldPatientId);
        jsonObject.addProperty("fldDoctorId", SPUtil.getUserId());
        jsonObject.addProperty("fldEditId", SPUtil.getUserId());
        jsonObject.addProperty("fldMedicalFlag", 0);//初复诊标识 初诊 = 0,复诊 = 1
        jsonObject.addProperty("fldMedicalTime", time);//病历时间（补写病历传入）
        UHttp.start(getServes().saveMedical(jsonObject), "病历保存(可同时签到接诊)", new HttpMsgListener<ResultBean<Integer>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                USnackbar.showWarning(context, describe);
            }

            @Override
            public void onFinish() {
                ProgressDialog.getInstance().stopProgressDialog();
            }

            @Override
            public void onSuccess(ResultBean<Integer> dto) {
                if (dto.isSuccess()) {
                    Intent intent = new Intent(context, RecordEditActivity.class);
                    intent.putExtra(Code.SP.ID, dto.getResult());
                    startActivity(intent);
                    finish();
                    sendBroadcast(new Intent(Code.Broadcast.CutoverToReception));
                } else {
                    ErrorBean errorBean = dto.getError();
                    if (errorBean != null) {
                        showNotice(UTxt.isNull(errorBean.getMessage(), Code.DefaultPrompt));
                    } else {
                        showNotice(Code.DefaultPrompt);
                    }
                }
            }
        });
    }

}
